// subpkg_worker/indoorActivities/indoorActivities.js
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    elderlyId:'',
    nurseId:'',
    index:'',
    otherName:'',
    fileList: [],
    activityList:
    [
      {
        activityType:'打麻将',
        duration:'',
        checked:false,
        image:'https://imagepphcloud.thepaper.cn/pph/image/210/657/398.jpg',
        picture:[],
        url:''
      },
      {
        activityType:'下象棋',
        duration:'',
        checked:false,
        image:'https://img2.baidu.com/it/u=735374085,798997103&fm=253&fmt=auto&app=138&f=JPEG?w=700&h=400',
        picture:[],
        url:''
      },
      {
        activityType:'书法',
        duration:'',
        checked:false,
        image:'https://pic.616pic.com/ys_bnew_img/00/11/43/pDphta3YFS.jpg',
        picture:[],
        url:''
      },
      {
        activityType:'打牌',
        duration:'',
        checked:false,
        image:'https://img1.baidu.com/it/u=2752089064,219852676&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=667',
        picture:[],
        url:''
      },
      {
        activityType:'其他',
        duration:'',
        checked:false,
        image:'https://img2.baidu.com/it/u=2225049421,3364197998&fm=253&fmt=auto&app=138&f=JPEG?w=888&h=500',
        picture:[],
        url:''
      }
    ],
  },

  afterRead(event) {
    var index = event.currentTarget.dataset.index
    this.setData({
      index:index
    })
    var fileList = this.data.fileList
    console.log(index)
    const that = this
    const { file } = event.detail;
   
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    var activityList = this.data.activityList
    activityList[index].picture.push(file)
    this.setData({
      activityList:activityList
    })
    
  },
  
  radioChange(e){
    var index = e.currentTarget.dataset.index
    const activityList = this.data.activityList
    activityList[index].checked=activityList[index].checked?false:true
    if(!activityList[index].checked){
      activityList[index].duration=''
    }
    this.setData({
      activityList:activityList
    })
  },
  bindinput(e){
    var index = e.currentTarget.dataset.index
    const activityList = this.data.activityList
    activityList[index].duration = e.detail.value
    this.setData({
      activityList:activityList
    })
  },
  bindinput1(e){
    var index = e.currentTarget.dataset.index
    this.setData({
      otherName:e.detail.value
    })
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  save(){
    for(var i = 0; i< this.data.activityList.length; i++){
      if(this.data.activityList[i].checked){
        this.my_request(this.data.activityList[i])
      }
    }
  },
  my_request(activity){
    if(activity.duration===''){
      Toast('请填写活动时间')
      return
    }
    if(activity.picture.length!=0){
      const that = this
      wx.uploadFile({
        url: 'https://zhixiao.site:8080/activity/upload', // 仅为示例，非真实的接口地址
        filePath: activity.picture[0].url,
        name: 'file',
        formData: { user: 'test' },
        success(res) {
          // 上传完成需要更新 fileList
          console.log(res)
          console.log(res.data)
          var url = res.data.slice(32,res.data.length-3)
          console.log(url)
          var activityList = that.data.activityList
          activity.url = url
          activityList[that.data.index].url = url
          that.setData({
             activityList:activityList
            });
        },
        complete:(res)=>{
        var url = activity.url
          wx.request({
            url: 'https://zhixiao.site:8080/activity/add',
            data:{
              elderlyId:this.data.elderlyId,
              nurseId:this.data.nurseId,
              activityType:activity.activityType,
              duration:activity.duration,
              pictureUrls:url
            },
            success:(res)=>{
              console.log(res)
              Toast('上传成功！')
            }
          })
        }
        
      });
    }
    else{
      var url = ''
      wx.request({
        url: 'https://zhixiao.site:8080/activity/add',
        data:{
          elderlyId:this.data.elderlyId,
          nurseId:this.data.nurseId,
          activityType:activity.activityType,
          duration:activity.duration,
          pictureUrls:url
        },
        success:(res)=>{
          console.log(res)
          Toast('上传成功！')
        }
      })
    }
    

  
    
   
  },
  onLoad(options) {
    this.setData({
      elderlyId:options.elderId,
      nurseId:wx.getStorageSync('userid')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})